<template>
  <div class="answers_object">
    <div class="bubble_object">
      <template v-if="ansContentFileListIsEmpty(answersData.data.ansContentFileList)">
        <template v-if="newPageUrl">
          <div><span @click="jumpUrl()" v-html="content"></span></div>
        </template>
        <template v-else>
          <div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span v-html="answersData.data.content"></span></div>
        </template>

      </template>
      <template v-else>
        <div class="bubble_titles">
          {{ answersData.data.question }}
        </div>
        <div v-for="item in answersData.data.ansContentFileList" :key="item.fileId">
          <em class="iconfont icon-fujian"/>
          <a target="_blank"
             @click="downloadFile(item.fileId, item.qaBankId, item.piid)"
             style="color: blue">{{ item.fileName }}</a>
        </div>
      </template>

    </div>
  </div>
</template>

<script>
import {isEmpty} from "../../../common/util";
import qs from "qs";
import AesGcm from "@/utils/aesgcm";
import {extractInnerTextFromHtml, extractUrlFromHtml} from "@/utils/extractFromHtml";

export default {
  name: 'AnswersObject',
  props: ['robotInfo', 'answersData'],
  data() {
    return {
      interfaceURL: '',
      newPageUrl: null,
      content: null
    }
  },
  created() {
    this.interfaceURL = AesGcm.decrypt(process.env.VUE_APP_INTERFACE_URL) + '/api/downloadFile.ajax/file.file?';
    this.newPageUrl = extractUrlFromHtml(this.answersData.data.content);
    this.content = extractInnerTextFromHtml(this.answersData.data.content);
  },
  methods: {
    ansContentFileListIsEmpty(fileList) {
      return isEmpty(fileList);
    },

    downloadFile(fileId, qaBankId, piid) {
      const param = qs.stringify({
        'fileId': fileId,
        'qaBankId': qaBankId,
        'piid': piid,
        'type': 'file'
      })
      MXCommon.download(this.interfaceURL + param);  //下载文件
    },
    // 外部网页链接跳转
    async jumpUrl() {
      MXCommon.openUrlPage(this.newPageUrl);
    },

  }
}
</script>
